package com.svo.weitu.util;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.svo.weitu.App;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * Created by Administrator on 2016/11/21.
 */
public class Utils {
    public static int getScreenWidth(){
        DisplayMetrics dm =App.context.getResources().getDisplayMetrics();
        int w_screen = dm.widthPixels;
        int h_screen = dm.heightPixels;
        return  w_screen;
//        Log.i(tag, "屏幕尺寸2：宽度 = " + w_screen + "高度 = " + h_screen + "密度 = " + dm.densityDpi);
    }
    /**
     *
     * @param context
     * @param dp
     * @return
     */
    public static int dip2px(Context context, float dp){
        final float scale=context.getResources().getDisplayMetrics().density;
        return(int)(dp*scale+0.5f);
    }

    /**
     *
     * @param context
     * @param px
     * @return
     */
    public static int px2dip(Context context, float px){
        final float scale=context.getResources().getDisplayMetrics().density;
        return(int)(px/scale+0.5f);
    }

    /**
     *
     * @param context
     * @param spValue
     * @return
     */
    public static int sp2px(Context context, float spValue){
        final float scale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue*scale + 0.5f);
    }

    /**
     *
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2sp(Context context, float pxValue){
        final float scale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue/scale + 0.5f);
    }
    /**
     * 获取版本号
     * @return 当前应用的版本号
     */
    public static String getVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "1.0";
        }
    }
    /**
     * 获取版本号
     * @return 当前应用的版本号
     */
    public static int getVersionCode(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            return info.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
    }
    /**
     * 复制内容到剪贴板
     * @param msg
     */
    public static void clip(String msg) {
        ClipboardManager cm = (ClipboardManager) App.context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (!TextUtils.isEmpty(msg))
            cm.setPrimaryClip(ClipData.newPlainText("已复制到剪贴板", msg));
        UiUtil.toast(App.context, "已复制到剪贴板", Toast.LENGTH_LONG);
    }





    /**
     * 换算时间,换算为日期格式,如:2012-10-29,昨天,前天,1分钟前等等
     * @param t 长整型时间
     */
    public static String formatDate(long t) {
        String strDate = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String systemTime = sdf.format(new Date()).toString();
        String time = sdf.format(t);

        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -1);
        String yesterday = sdf.format(c.getTime()).toString();
        c.add(Calendar.DAY_OF_MONTH, -1);
        String yesterday_before = sdf.format(c.getTime()).toString();

        try {
            Date begin = sdf.parse(time);
            Date end = sdf.parse(systemTime);
            long between = (end.getTime() - begin.getTime()) / (1000 * 60);
            if (time.substring(0, 10).equals(yesterday.substring(0, 10))) {
                strDate = "昨天";
            } else if (time.substring(0, 10).equals(
                    yesterday_before.substring(0, 10))) {
                strDate = "前天";
            } else if (between <= 0) {
                strDate = "1分钟前";
            } else if (between < 60 && between > 0) {
                strDate = Math.round(between) + "分钟前";
            } else if (between >= 60 && between < 60 * 24) {
                strDate = Math.round(between / 60) + "小时前";
            } else {
                strDate = time.substring(0, 10);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return strDate;
    }
    /**
     * 网络是否可用
     * @param context
     * @return
     */
    public static boolean isNetworkAvailable(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (mConnectivityManager == null) {
                return false;
            }else {
                NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
                if (mNetworkInfo != null) {
                    return mNetworkInfo.isAvailable();
                }
            }
        }
        return false;
    }

    /**
     * wifi是否可用
     * @param context
     * @return
     * @throws Exception
     */
    public static boolean isWifiDataEnable(Context context) throws Exception {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean isWifiDataEnable = false;
        isWifiDataEnable = connectivityManager.getNetworkInfo(
                ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
        return isWifiDataEnable;
    }

    /**
     * 关闭软键盘
     */
    public static void closeSoftKeyboard(Context context) {
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null && ((Activity) context).getCurrentFocus() != null) {
            inputMethodManager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus()
                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
    /**
     * 判断软键盘是否弹出
     */
    public static boolean isSHowKeyboard(Context context, View v) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
        if (imm.hideSoftInputFromWindow(v.getWindowToken(), 0)) {
            imm.showSoftInput(v, 0);
            return true;
            //软键盘已弹出
        } else {
            return false;
            //软键盘未弹出
        }
    }
    /**
     * 获取当前的时间
     * @return
     */
    public static String getTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        int h = calendar.get(Calendar.HOUR_OF_DAY);
        int m = calendar.get(Calendar.MINUTE);
        StringBuilder sb = new StringBuilder();
        sb.append(h<10?"0"+h:h);
        sb.append(":");
        sb.append(m<10?"0"+m:m);
        return sb.toString();
    }

}
